<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构的内容 -->
      <el-card class="tree-card">
        <tree-tools
          :tree-node="company"
          :is-root="true"
          @addDepts="addDepts"
        />
        <!-- 树形控件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{data}"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <add-dept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :tree-node="node"
        @addDepts="getDepartments"
      />

    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept' // 引入新增部门组件
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {},
      /* departs设置树型控件的数据内容 */
      departs: [],
      /* 设置属性要读取的字段 */
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 显示窗体
      node: null
    }
  },
  created() {
    this.getDepartments() // 调用自身的方法
  },
  methods: {
    /* 获取列表信息 */
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '') // 需要将其转化成树形结构
    },
    /* 监听treetool组件点击新增子部门事件 node是当前组件的详细数据*/
    addDepts(node) {
      /* 1. 弹出窗口 */
      this.showDialog = true
      /* 2. 将点击的子节点数据保存 */
      this.node = node
    },
    /* 监听treetool组件点击编辑按钮事件 */
    editDepts(node) {
      /* 1. 弹出窗口 */
      this.showDialog = true
      /* 2. 将点击的子节点数据保存 */
      this.node = node
      /* 3. 父组件调用子组件里的方法时,需用通过ref来获取子组件的实例对象来调用方法 */
      this.$refs.addDept.getDepartDetail(this.node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
